#!/bin/sh
# 处理所有 .template 结尾的文件
for template in /etc/nginx/conf.d/*.template; do
    [ ! -f "$template" ] && echo "No template files found" && continue
    # 将输出文件名改为 .conf 结尾
    conf="${template%.template}.conf"
    echo "Processing $template"
    if envsubst '$SERVER_IP $SERVER_PORT' < "$template" > "$conf"; then
        echo "Replaced $conf successfully"
    else
        echo "Failed to replace $conf"
    fi
done
# Check if the index.html file exists before processing
if [ -f "/usr/share/nginx/html/index.html" ]; then
    # Use envsubst to replace environment variable placeholders
    echo "Processing /usr/share/nginx/html/index.html"
    envsubst < /usr/share/nginx/html/index.temp > /usr/share/nginx/html/index.html.tmp
    mv /usr/share/nginx/html/index.html.tmp /usr/share/nginx/html/index.html
    echo "Processed content:"
    cat /usr/share/nginx/html/index.html
else
    echo "Info: /usr/share/nginx/html/index.html does not exist , skip replace env"
    exit 1
fi

# Run nginx to serve static files
exec nginx -g "daemon off;"